{% comment %}
<!--
Example code for using Matamo as alternative analytics solution.
-->
{% if site.matomo_analytics %}
<script>
  var _paq = _paq || [];

  {% if site.matomo_analytics.no_cookies %}
  _paq.push(['disableCookies']);
  {% endif %}

  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  _paq.push(['setTrackerUrl', u+'piwik.php']);
  _paq.push(['setSiteId', '{{site.matomo_analytics.site_id}}']);

  var pushStateEl = document.getElementsByTagName('hy-push-state')[0];
  var timeStartLoadPage, referer, timeItTookToLoadPage;

  pushStateEl.addEventListener('hy-push-state-start', function() {
    timeStartLoadPage = new Date().getTime();
    referer = window.location.toString();
  });

  pushStateEl.addEventListener('hy-push-state-ready', function() {
    timeItTookToLoadPage = new Date().getTime() - timeStartLoadPage;
  });

  pushStateEl.addEventListener('hy-push-state-after', function() {
    _paq.push(['setReferrerUrl', referer]);
    _paq.push(['setCustomUrl', window.location.toString()]);
    _paq.push(['setDocumentTitle', document.title]);
    _paq.push(['deleteCustomVariables', 'page']);
    _paq.push(['setGenerationTimeMs', timeItTookToLoadPage]);
    _paq.push(['trackPageView']);
  });

  window.loadJSDeferred('{{site.matomo_analytics.root}}piwik.js');
</script>
{% endif %}
{% endcomment %}
